$(document).ready(function () {
    var cnName;       //名称
    var style;       //展会行业
    var status  ;        //开展状态
    var startYear;        //年份
    var startMonth = '';    //月份
    var pageNum = '';
    var pageSize = 9;
    var tol;
    function params() {
        var ps="&pageSize="+pageSize;
        var par = "";
        //TODO 如果 cnName不为空
        if ($("#seek-in").val()===undefined) {      //展会名
            par ="&"+"cnName";
        } else if ($("#seek-in").val()==="") {
            par ="&"+"cnName";
        }else{
            par += "&"+"cnName="+cnName;
        }
        //TODO 如果 style不为空
        if ( $("#polis").text()==="行业") {
            par += "";
        }else {
            par +="&"+"style="+style;
        }
        if ($("#vintage").text()==="年份"){
            par += "";
        }else {
            par +="&"+"startYear="+startYear;
        }
        if ($("#month").text()==="月份"){
            par += "";
        }else {
            par +="&"+"startMonth="+startMonth;
        }
        //......
        //如果par的第一个字符为 & 字符，则去除第一个 & 字符
        if (par[0]==="&") {
            par=par.substring(1,par.length);
        }
        return par+ps;

    }


    function listdata(str) {
        $.ajax({
            type: "GET",
            url: "http://www.enet720.net:8080/zwkj/plan/exhibtions",
            contentType: 'application/json;charset=UTF-8',
            dataType: 'json',
            async: false,
            data: str,
            success: function (data) {
                var d = data.data;
                // 如果数据查询成功，但是数据为空的判断处理
                if (!(d == null)) {
                    tol = Math.ceil(d.total / 9);
                    if (tol<1){
                        tol=1;
                    }
                    var list = d.list;
                    for (var i = 0; i < list.length; i++) {
                        $("#plans").append("      <li>\n" +
                            "                    <div class=\"row\" >\n" +
                            "                        <div class=\"col-xs-2 col-sm-2 col-md-2 col-lg-1\">\n" +
                            "                            <span class=\"elli\">" + list[i].id + "</span>\n" +
                            "                        </div>\n" +
                            "                        <div class=\"col-xs-2 col-sm-2 col-md-2 col-lg-3\">\n" +
                            "                            <span class=\"elli\" title='" + list[i].cnName + "（" + list[i].enName + "）'>" + list[i].cnName + "（" + list[i].enName + "）</span>\n" +
                            "                        </div>\n" +
                            "                        <div class=\"col-xs-2 col-sm-2 col-md-2 col-lg-2\">\n" +
                            "                            <span class=\"elli\" title='" + list[i].startTime + "  至 " + list[i].entTime + "'>" + list[i].startTime + "  至 " + list[i].entTime + "</span>\n" +
                            "                        </div>\n" +
                            "                        <div class=\"col-xs-2 col-sm-2 col-md-2 col-lg-2\">\n" +
                            "                            <span class=\"elli\">" + list[i].hall + "</span>\n" +
                            "                        </div>\n" +
                            "                        <div class=\"col-xs-2 col-sm-2 col-md-2 col-lg-2\">\n" +
                            "                            <span class=\"elli\" title='" + list[i].hostUnit + "'>" + list[i].hostUnit + "</span>\n" +
                            "                        </div>\n" +
                            "                        <div class=\"col-xs-2 col-sm-2 col-md-2 col-lg-2\">\n" +
                            "                            <span class=\"elli\">" + list[i].style + "</span>\n" +
                            "                        </div>\n" +
                            "                    </div>\n" +
                            "                </li>")
                    }
                } else {
                    $("#plans").append("抱歉，你的查找的数据不存在！嘿嘿嘿........请从新查找<a href=\"javascript:\" onclick=\"location.reload();\">点击返回</a>！");
                    tol = 1;
                }
            },
            error: function () {
                alert("出错了,抱歉！")
            }
        });
    }

    listdata("pageSize=9");

    $.jqPaginator('#pagination', {
        totalPages: tol,
        visiblePages: 10,
        currentPage: 1,
        wrapper: '<ul class="pagination" style="margin: 0 0"></ul>',
        first: '<li class="first"><a href="javascript:void(0);">首页</a></li>',
        prev: '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
        next: '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
        last: '<li class="last"><a href="javascript:void(0);">最后一页</a></li>',
        page: '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
        onPageChange: function (num) {
            var param = params();
            $("#plans").empty();
            listdata(param+"&pageNum="+num);
        }
    });

    $("a[name='country']").click(function () {     //国家(默认中国)
        var coun=$(this).text().toString();
        $("#ctry").text(coun);
    });
    $("a[name='urban']").click(function () {     //行业检索
        style=$(this).text().toString();
        $("#polis").text(style);
        var param = params();
        $("#plans").empty();
        listdata(param);
    });
    $("a[name='year']").click(function () {        //年份检索
        startYear=$(this).text().toString();
        $("#vintage").text(startYear);
        var param = params();
        $("#plans").empty();
        listdata(param);
        $('#pagination').jqPaginator("option", {
            totalPages: tol
        });
    });

    $("a[name='may']").click(function () {         //月份检索
        startMonth=$(this).text().toString();
        $("#month").text(startMonth);
        var param = params();
        $("#plans").empty();
        listdata(param);
        $('#pagination').jqPaginator("option", {
            totalPages: tol
        });
    });
    $("#seek-bt").click(function () {           //关键字检索
        cnName=$("#seek-in").val();
        var param = params();
        $("#plans").empty();
        listdata(param);
        $('#pagination').jqPaginator("option", {
            totalPages: tol
        });
    });


});

